; Bootflub
; by Anshu Chimala
;   This is Starflub's bootloader
;   Because we got sick of GRUB ;)

[BITS 16]	; 16 bit code
[ORG 0x7C00]	; origin set to 0x7C00

start:
	jmp main				; jump over data to main
	nop
	times 0x3B db 0	; makes it mountable...
	
;;;;;; data
; functions
wkc:
	xor al,al
	in al, 0x64
	test al, 2
	jnz wkc
	ret

wkf:
	xor cx,cx
	in al, 0x64
	test al, 1
	jz wkf
	ret

halt:
	mov byte [gs:0],al
	mov byte [gs:1],0x04
	cli
	hlt

;load kernel.bin from disk to 0x100000
read:
	xor ax,ax                     ; Floppy Reset BIOS Function
	mov dl,[drive]                ; Select floppy that was booted from
	int 0x13 
	jc read

	mov ax,0xffff 
	mov es,ax                     ; Data buffer for file
	mov bx,0x10                   ; Start of segment
	mov ah,2        	      ; Function to read disk
	mov al,17                     ; Total sectors to read
	mov ch,0                      ; Track
	mov cl,2                      ; Sector
	mov dh,0                      ; Head | Drive is already loaded
	int 0x13                      ; Call BIOS read disk function
	jc read 	              ; motor error, try again

	mov ax,0xffff 
	mov es,ax
	mov bx, 0x2210
	mov ah,2        	      ; Function to read disk
	mov al,18                     ; Total sectors to read
	mov ch,0                      ; Track
	mov cl,1                      ; Sector
	mov dh,1                      ; Head | Drive is already loaded
	int 0x13                      ; Call BIOS read disk function

	mov dx,0x3F2                  ; stop the motor
	mov al,0x0C                   ; from spinning
	out dx,al

;;;;;; main program starts here

main:

	mov ax, 0x0000	; data segment register
	mov ds, ax	; has to be done in 2 steps (cpu limitation)
	
	;; important, load kernel
	call read
	
	jmp $		; don't touch this, inf loop.

;;;;;; main program ends here

monitor_write:
	mov ah, 0x0E	; BIOS function, put char
	mov bh, 0x00	; page number
	mov bl, 0x07	; white fg, black bg attribute
.nextchar
	lodsb
	or al, al	; check for end of string
	jz .return	; if so, return
	int 0x10	; otherwise, call BIOS video interrupt
	jmp .nextchar	; go to next char
.return
	ret		; return.

;;;;;; Data



;;;;;; stop coding now...

; Fill the rest of the bootloader with 0's
; to make it 512 bytes
; And tack on an 0xAA55 at the end.
times 510-($-$$) db 0
signature dw 0xAA55

